# -*- mode: python -*-
###
#
# (c) 2000-2007 Wijnand Modderman <wijnand at tehmaze dot com>
# All rights reserved. See docs/LICENSE
#
###

__revision__ = '$Hg:$'

# DDB imports
from ddb.log import log
from ddb.register import register
from ddb.db import db
from raw.feedback import feedback

# DDB exports
EXPORT=['Karma']

class Karma:
    def __init__(self):
        self.reg = register.init('karma')   # register for this module
        self.ctx = []                       # context, default 'general'
        self._db = None
        self._initDb()

    def _initDb(self):
        if self.reg.get('brand'):
            if self._db:
                self._db.disconnect()
            self._db = db(self.reg.get('brand')[0])

    def _configDb(self, event):
        if not self._db and self.reg.get('brand'):
            self._initDb()
            return True
        else:
            event.reply('Please ask an admin to configure a database for this module. (hint: register set karma brand "brand")')
            return False

    def karma(self, event):
        """[<item>]

        Look up the karma of <item>."""
        item = None
        if not event.args:
            if not event.user.username:
                if not event.user.nick:
                    return event.reply(syntax())
                else:
                    item = event.user.nick
            else:
                item = event.user.username
        else:
            item = event.args
        if not self._db and not self._configDb(event):
            return
        karma = self._db.get('karma', 
            'karma', 'altcount',            # select these fields
            where={'term': ['=', item]})    # with these criteria
        if not karma:
            event.reply('the karma of \002%s\002 is 0.00' % item)
        else:
            event.reply('the karma of \002%s\002 is %.02f' % (item, karma[0][0]))

# vim:ts=4:sw=4:et:
